package com.dz.reggie.utils;


import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;

/**
 * 短信发送工具类
 */
@Slf4j
public class SMSUtils {

    /**
     * 发送短信
     * @param signName 签名
     * @param templateCode 模板
     * @param phoneNumbers 手机号
     * @param param 参数
     */
    /**
     * public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
     * // 1、初始化配置对象 com.aliyuncs.profile.DefaultProfile
     * // 用户登录名称 reggie@1917735513642170.onaliyun.com
     * // AccessKey ID LTAI5tEtK3LgxY54QC9FxqXH
     * // AccessKey Secret VX9H7Wd5ZLRaevr93ubFHzYa0sCUfQ
     * DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou",
     * "LTAI5tEtK3LgxY54QC9FxqXH",
     * "VX9H7Wd5ZLRaevr93ubFHzYa0sCUfQ");
     * // 实例化一个客户端，从 com.aliyuncs.IAcsClient 类生成对象 client
     * IAcsClient client = new DefaultAcsClient(profile);
     * // 创建对应 API 的 Request
     * SendSmsRequest request = new SendSmsRequest();
     * // 设置请求类 request 的参数
     * request.setSysRegionId("cn-hangzhou");
     * <p>
     * request.setPhoneNumbers(phoneNumbers);
     * request.setSignName(signName);
     * <p>
     * request.setTemplateCode(templateCode);
     * request.setTemplateParam("{\"code\":\""+param+"\"}");
     * try {
     * // 调用 response 中对应的 get 方法获得返回的参数值
     * SendSmsResponse response = client.getAcsResponse(request);
     * System.out.println("短信发送成功");
     * }catch (ClientException e) {
     * System.out.println("ErrCode:" + e.getErrCode());
     * System.out.println("ErrMsg:" + e.getErrMsg());
     * System.out.println("RequestId:" + e.getRequestId());
     * }
     * }
     **/

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                // 必填，您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 必填，您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }

    public static boolean sendMessage(String signName, String templateCode, String phoneNumbers, String param) throws Exception {
        // 工程代码泄露可能会导致 AccessKey 泄露，并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用，仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client =
                SMSUtils.createClient("xxxxxxxx", "xxxxxxxx");
        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setSignName(signName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phoneNumbers)
                .setTemplateParam(param);
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
            log.info("sendSmsResponse：{}", sendSmsResponse.toString());
            return true;

        }catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 如有需要，请打印 error
            com.aliyun.teautil.Common.assertAsString(error.message);
            return false;
        }
    }
}



